// UI相关的帮助方法
import {UiDef} from './ui.d'
import {useQuasar, QVueGlobals} from 'quasar'
import i18n from '../i18n/i18n'

let $q: QVueGlobals | null = null

// 准备notify
export function prepareNotify() {
  $q = useQuasar()
}

const {t} = i18n.global
export default {
  // 失败通知
  showNotifyFailed: function (message) {
    $q?.notify({
      message: message,
      classes: 'bg-negative-lighten',
      position: 'bottom-right',
      actions: [{
        label: t('关闭'), color: 'white', handler: () => { /* ... */
        },
      }],
      timeout: 15000,
    })
  },

  // 成功通知
  showNotifySucceeded: function (message) {
    $q?.notify({
      message: message,
      classes: 'bg-positive',
      position: 'bottom-right',
      actions: [{
        label: t('关闭'), color: 'white', handler: () => { /* ... */
        },
      }],
      timeout: 5000,
    })
  },
  //显示消息框
  messageBox: () => {
    console.warn('showMsgBox: MessageBox组件未初始化')
    return Promise.resolve('')
  },
  //关闭消息框
  closeMsgBox: () => {
    console.warn('hideMsgBox: MessageBox组件未初始化')
  },
  // 显示出错列表框
  showErrorListDlg: () => {
    console.warn('showErrorListDlg: ErrorListDisplay组件未初始化')
  },
  // 隐藏出错列表框
  hideErrorListDlg: () => {
    console.warn('hideErrorListDlg: ErrorListDisplay组件未初始化')
  },
  // 单输入框
  singleInputBox: () => {
    console.warn('singleInputBox: SingleInputBox组件未初始化')
  },
  // 选中输入框文本
  select: function (event: Event) {
    const element = event.target as HTMLInputElement
    if (typeof element.select === 'function') {
      element.select()
    }
  },
  // 显示全屏加载信息
  showScreenLoading: function (message?: string) {
    $q?.loading?.show({
      message: message ?? '请稍候...',
      boxClass: 'bg-white text-black',
      spinnerColor: 'primary',
      spinnerSize: 50
    })
  },
  // 隐藏全屏加载信息
  hideScreenLoading: function () {
    $q?.loading?.hide()
  },
} as UiDef
